package com.example.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.IdWorker;
import com.example.common.R;
import com.example.domain.Category;
import com.example.domain.Dish;
import com.example.domain.DishFlavor;
import com.example.dto.DishDto;
import com.example.service.CategoryServicce;
import com.example.service.DishFlavorService;
import com.example.service.DishService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.core.Local;
import org.springframework.context.annotation.Bean;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.yaml.snakeyaml.events.SequenceEndEvent;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/dish")
@ResponseBody
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private CategoryServicce categoryServicce;
    @GetMapping("/page")
    public R<Page> getDishPage(int page,int pageSize,String name)
    {
        int pageCount=(page-1)*pageSize;
        Page<DishDto> dto=new Page<>();//创建dto对象
        if(name==null)
        {
            Page<Dish> p=new Page<>(page,pageSize);
            List<Dish> selectall = dishService.selectall(pageCount, pageSize);
            p.setRecords(selectall);
            p.setTotal(dishService.selectcount());

            BeanUtils.copyProperties(p,dto,"records");//对象拷贝，不拷贝records
            List<Dish> records = p.getRecords();//得到查询道德dish的数据
            List<DishDto> dishdto=new ArrayList<>();//创建dishdto的表

            for(int i=0;i<records.size();i++)
            {
                Dish dish = records.get(i);
                DishDto dishDto=new DishDto();
                BeanUtils.copyProperties(dish,dishDto);
                Long categoryId = dish.getCategoryId();
                Category category = categoryServicce.selectbycategoryId(categoryId);
                String name1 = category.getName();
                dishDto.setCategoryName(name1);
                dishdto.add(dishDto);
            }
            dto.setRecords(dishdto);//把dishdto的表设置给dto的page对象
            return R.success(dto);
        }
        else
        {
            Page<Dish> p=new Page<>(page,pageSize);
            List<Dish> selectbyname = dishService.selectbyname(pageSize, pageCount, name);
            p.setRecords(selectbyname);

            p.setTotal(dishService.selectcount());
            BeanUtils.copyProperties(p,dto,"records");//对象拷贝，不拷贝records
            List<Dish> records = p.getRecords();//得到查询道德dish的数据
            List<DishDto> dishdto=new ArrayList<>();//创建dishdto的表

            for(int i=0;i<records.size();i++)
            {
                Dish dish = records.get(i);
                DishDto dishDto=new DishDto();
                BeanUtils.copyProperties(dish,dishDto);
                Long categoryId = dish.getCategoryId();
                Category category = categoryServicce.selectbycategoryId(categoryId);
                String name1 = category.getName();
                dishDto.setCategoryName(name1);
                dishdto.add(dishDto);
            }
            dto.setRecords(dishdto);//把dishdto的表设置给dto的page对象
            return R.success(dto);
        }
    }

    @PostMapping
    @Transactional
    public R<DishDto> save(HttpServletRequest request, @RequestBody DishDto dishDto)
    {
        Long getId = (Long) request.getSession().getAttribute("employee");
        IdWorker idWorker=new IdWorker();
        long dishid = idWorker.nextId();
        dishDto.setId(dishid);
        dishDto.setCreateUser(getId);
        dishDto.setUpdateUser(getId);
        dishDto.setCreateTime(LocalDateTime.now());
        dishDto.setUpdateTime(LocalDateTime.now());
        log.info(dishDto.getId()+":"+dishDto.getName()+":"+dishDto.getCategoryId()+":"+dishDto.getPrice()+":"+dishDto.getCode()+":"+dishDto.getImage()+":"+dishDto.getDescription()+":"+dishDto.getStatus()+":"+dishDto.getSort()+":"+dishDto.getCreateTime()+":"+dishDto.getCreateUser()+":"+dishDto.getUpdateTime()+":"+dishDto.getUpdateUser());
        dishService.save(dishDto);

        List<DishFlavor> flavors = dishDto.getFlavors();
        for(int i=0;i<flavors.size();i++)
        {
            DishFlavor dishFlavor = flavors.get(i);
            dishFlavor.setDishId(dishid);
            dishFlavor.setId(idWorker.nextId());
            dishFlavor.setCreateUser(getId);
            dishFlavor.setUpdateUser(getId);
            dishFlavor.setCreateTime(LocalDateTime.now());
            dishFlavor.setUpdateTime(LocalDateTime.now());
            dishFlavorService.savefalvor(dishFlavor);
        }
        return R.success(dishDto);
    }

    @GetMapping("/{id}")
    public R<DishDto> selectDishAndFlavor(@PathVariable Long id)
    {
        DishDto dishDto=new DishDto();
        Dish selectbyid = dishService.selectbyid(id);
        log.info("当前查询的dish信息："+selectbyid.toString());
        BeanUtils.copyProperties(selectbyid,dishDto);
//        dishDto.setId(selectbyid.getId());
//        dishDto.setName(selectbyid.getName());
//        dishDto.setCategoryId(selectbyid.getCategoryId());
//        dishDto.setPrice(selectbyid.getPrice());
//        dishDto.setCode(selectbyid.getCode());
//        dishDto.setImage(selectbyid.getImage());
//        dishDto.setDescription(selectbyid.getDescription());
//        dishDto.setStatus(selectbyid.getStatus());
//        dishDto.setSort(selectbyid.getSort());
//        dishDto.setCreateUser(selectbyid.getCreateUser());
//        dishDto.setCreateTime(selectbyid.getCreateTime());
//        dishDto.setUpdateTime(selectbyid.getUpdateTime());
//        dishDto.setUpdateUser(selectbyid.getUpdateUser());
//        dishDto.setIsDeleted(selectbyid.getIsDeleted());

        List<DishFlavor> seelctbydishid = dishFlavorService.seelctbydishid(id);
        dishDto.setFlavors(seelctbydishid);
        return R.success(dishDto);
    }

    @PutMapping
    public R<DishDto> update(HttpServletRequest request,@RequestBody DishDto dishDto)
    {
        Long getId = (Long) request.getSession().getAttribute("employee");
        dishDto.setUpdateUser(getId);
        dishDto.setUpdateTime(LocalDateTime.now());
        dishService.update(dishDto);
        List<DishFlavor> flavors = dishDto.getFlavors();
        boolean f=true;
        IdWorker idWorker=new IdWorker();
        List<String> getnamebydishid = dishFlavorService.getnamebydishid(dishDto.getId());
        List<String> a=new ArrayList<>();
        for(int i=0;i<getnamebydishid.size();i++)
        {
            a.add(getnamebydishid.get(i));
        }
        log.info(a.toString());
        log.info(flavors.toString());
        for(int i=0;i<flavors.size();i++)
        {
            f=true;
            DishFlavor dishFlavor = flavors.get(i);
            dishFlavor.setUpdateTime(LocalDateTime.now());
            dishFlavor.setUpdateUser(getId);
            for(int j=0;j<getnamebydishid.size();j++)
            {
                String s = getnamebydishid.get(j);
                if(s.equals(dishFlavor.getName()))
                {
                    f=false;
                    break;
                }
            }
            if(f)
            {
                dishFlavor.setDishId(dishDto.getId());
                dishFlavor.setId(idWorker.nextId());
                dishFlavor.setCreateUser(getId);
                dishFlavor.setCreateTime(LocalDateTime.now());
                dishFlavorService.savefalvor(dishFlavor);
            }
            else
            {
                dishFlavorService.update(dishFlavor);
                a.remove(dishFlavor.getName());
            }
        }
        log.info("-------------------+");
        log.info(a.toString());
        for(int i=0;i<a.size();i++)
        {
            String s = a.get(i);
            dishFlavorService.deletebynameanddishid(s,dishDto.getId());
        }
        return R.success(dishDto);
    }

    @PostMapping("/status/{status}")
    public R<Dish> dishStatusByStatus(HttpServletRequest request,@PathVariable int status,String ids)
    {
        Long getId = (Long) request.getSession().getAttribute("employee");
        String[] idlist=ids.split(",");
        List<Long> id=new ArrayList<>();
        for(int i=0;i<idlist.length;i++)
        {
            id.add(Long.valueOf(idlist[i]));
        }
        for(int i=0;i<id.size();i++)
        {
            Dish dish=new DishDto();
            dish.setId(id.get(i));
            dish.setStatus(status);
            dish.setUpdateTime(LocalDateTime.now());
            dish.setUpdateUser(getId);
            dishService.dishStatusByStatus(dish);
        }
        return R.success(null);
    }

    @DeleteMapping
    public R<Dish> deleteDish(String ids)
    {
        String[] split = ids.split(",");
        List<Long> id=new ArrayList<>();
        for(int i=0;i<split.length;i++)
        {
            id.add(Long.valueOf(split[i]));
        }
        for(int i=0;i<id.size();i++)
        {
            dishService.deleteDish(id.get(i));
        }
        return R.success(null);
    }

    @GetMapping("/list")
    public R<List<DishDto>> queryDishList(Long categoryId,Integer status)
    {
        if(status==null)
        {
            List<DishDto> dishDtos=new ArrayList<>();
            List<Dish> selectbycategoryid = dishService.selectbycategoryid(categoryId);
            for(int i=0;i<selectbycategoryid.size();i++)
            {
                //BeanUtils.copyProperties(dishDtos,selectbycategoryid);
                DishDto dishDto=new DishDto();
                Dish dish = selectbycategoryid.get(i);
                dishDto.setId(dish.getId());
                dishDto.setName(dish.getName());
                dishDto.setCategoryId(dish.getCategoryId());
                dishDto.setPrice(dish.getPrice());
                dishDto.setCode(dish.getCode());
                dishDto.setImage(dish.getImage());
                dishDto.setDescription(dish.getDescription());
                dishDto.setStatus(dish.getStatus());
                dishDto.setSort(dish.getSort());
                dishDto.setCreateTime(dish.getCreateTime());
                dishDto.setUpdateTime(dish.getUpdateTime());
                dishDto.setCreateUser(dish.getCreateUser());
                dishDto.setUpdateUser(dish.getUpdateUser());
                dishDto.setIsDeleted(dish.getIsDeleted());
                dishDtos.add(dishDto);
            }
            return R.success(dishDtos);
        }
        else
        {
            Dish dish1=new Dish();
            dish1.setCategoryId(categoryId);
            dish1.setStatus(status);
            List<DishDto> dishDtos=new ArrayList<>();
            List<Dish> selectbycategoryidandstatus = dishService.selectbycategoryidandstatus(dish1);
            for(int i=0;i<selectbycategoryidandstatus.size();i++)
            {
                //BeanUtils.copyProperties(dishDtos,selectbycategoryidandstatus);
                DishDto dishDto=new DishDto();
                Dish dish = selectbycategoryidandstatus.get(i);
                dishDto.setId(dish.getId());
                dishDto.setName(dish.getName());
                dishDto.setCategoryId(dish.getCategoryId());
                dishDto.setPrice(dish.getPrice());
                dishDto.setCode(dish.getCode());
                dishDto.setImage(dish.getImage());
                dishDto.setDescription(dish.getDescription());
                dishDto.setStatus(dish.getStatus());
                dishDto.setSort(dish.getSort());
                dishDto.setCreateTime(dish.getCreateTime());
                dishDto.setUpdateTime(dish.getUpdateTime());
                dishDto.setCreateUser(dish.getCreateUser());
                dishDto.setUpdateUser(dish.getUpdateUser());
                dishDto.setIsDeleted(dish.getIsDeleted());

                Long id = dish.getId();
                List<DishFlavor> seelctbydishid = dishFlavorService.seelctbydishid(id);
                //BeanUtils.copyProperties(dishDtos,seelctbydishid);
                dishDto.setFlavors(seelctbydishid);
                dishDtos.add(dishDto);
            }
            log.info("-----------------------");
            log.info(dishDtos.toString());
            return R.success(dishDtos);
        }
    }
}
